/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanel_XemMail.java
 *
 * Created on May 27, 2009, 9:23:59 AM
 */

package DAO;

import doanlythuyet_javaoutlook.MyUserControl.*;
import BUS.MyMailBUS;
import DTO.MyMailDTO;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;


/**
 *
 * @author Dang Thi Phuong Thao
 */
public class JPanel_XemMail_1 extends javax.swing.JPanel {

    private Boolean tatCaDuocChon = false;
    /** Creates new form JPanel_XemMail */
    public JPanel_XemMail_1() {
        initComponents();
        //DuaDanhSachMailVaoBang(DanhSachMail);
    }

    public void ThemDongVaoBang (JTable table, Object[][] CacGiaTri){
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        //model
    }
    public void XoaDongTrongBang (JTable table, int[] Indexs){
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        //model.
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel_DanhSachMail = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_danhsachmail = new javax.swing.JTable();
        jPanel_ChiTietMail = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane_chitietmail = new javax.swing.JEditorPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel_chude = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel_goitu = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel_ngay = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jPanel_DanhSachMail.setName("jPanel_DanhSachMail"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable_danhsachmail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Tất cả", "Chủ đề", "Người gởi", "Ngày"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable_danhsachmail.setCellSelectionEnabled(true);
        jTable_danhsachmail.setName("jTable_danhsachmail"); // NOI18N
        jTable_danhsachmail.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_danhsachmailMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_danhsachmail);
        jTable_danhsachmail.getColumnModel().getColumn(0).setMinWidth(40);
        jTable_danhsachmail.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable_danhsachmail.getColumnModel().getColumn(0).setMaxWidth(40);
        jTable_danhsachmail.getColumnModel().getColumn(1).setPreferredWidth(150);

        javax.swing.GroupLayout jPanel_DanhSachMailLayout = new javax.swing.GroupLayout(jPanel_DanhSachMail);
        jPanel_DanhSachMail.setLayout(jPanel_DanhSachMailLayout);
        jPanel_DanhSachMailLayout.setHorizontalGroup(
            jPanel_DanhSachMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
        );
        jPanel_DanhSachMailLayout.setVerticalGroup(
            jPanel_DanhSachMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
        );

        jSplitPane1.setTopComponent(jPanel_DanhSachMail);

        jPanel_ChiTietMail.setName("jPanel_ChiTietMail"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jEditorPane_chitietmail.setName("jEditorPane_chitietmail"); // NOI18N
        jScrollPane2.setViewportView(jEditorPane_chitietmail);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin chi tiết mail đang chọn:"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setName("jLabel1"); // NOI18N

        jLabel_chude.setName("jLabel_chude"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        jLabel_goitu.setName("jLabel_goitu"); // NOI18N

        jLabel4.setName("jLabel4"); // NOI18N

        jLabel_ngay.setName("jLabel_ngay"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel_chude, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel_goitu)
                        .addGap(117, 117, 117)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel_ngay)))
                .addContainerGap(448, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel_chude, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel_goitu)
                    .addComponent(jLabel4)
                    .addComponent(jLabel_ngay)))
        );

        javax.swing.GroupLayout jPanel_ChiTietMailLayout = new javax.swing.GroupLayout(jPanel_ChiTietMail);
        jPanel_ChiTietMail.setLayout(jPanel_ChiTietMailLayout);
        jPanel_ChiTietMailLayout.setHorizontalGroup(
            jPanel_ChiTietMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
        );
        jPanel_ChiTietMailLayout.setVerticalGroup(
            jPanel_ChiTietMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_ChiTietMailLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE))
        );

        jSplitPane1.setRightComponent(jPanel_ChiTietMail);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 538, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        try {
            // TODO add your handling code here:
            MyMailBUS busMail = new MyMailBUS();
            ArrayList<MyMailDTO> DanhSachMail = busMail.layTatCaMail();
            DuaDanhSachMailVaoBang(DanhSachMail);
        } catch (IOException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formAncestorAdded

    private void jTable_danhsachmailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_danhsachmailMouseClicked
        // TODO add your handling code here:
        //  TableColumnModel colModel = jTable_danhsachmail.getColumnModel();
        //  int columnModelIndex = colModel.getColumnIndexAtX(evt.getX());
        //  int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
        //  JOptionPane.showMessageDialog(null, modelIndex);

    }//GEN-LAST:event_jTable_danhsachmailMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jEditorPane_chitietmail;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel_chude;
    private javax.swing.JLabel jLabel_goitu;
    private javax.swing.JLabel jLabel_ngay;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_ChiTietMail;
    private javax.swing.JPanel jPanel_DanhSachMail;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable jTable_danhsachmail;
    // End of variables declaration//GEN-END:variables

    public void DuaDanhSachMailVaoBang(ArrayList<MyMailDTO>DanhSachMail){
       // DanhSachMail.get(1);
       /*Tabl tableMode = jTable_danhsachmail.getModel();

        TableColumnModel columnModel = jTable_danhsachmail.getColumnModel();
        TableColumn column= columnModel.getColumn(1);
        int countColum = jTable_danhsachmail.getColumnCount();
        int i =0;
        int j = 1;
        for(;j<countColum;j++)
        {
            //TableCellEditor cellEditor =jTable_danhsachmail
        }
    */
            Icon icon = new ImageIcon("C:\\1.png");
            final String cacCotDuLieu[] = {"Tất cả","Tình trạng","Đính kèm","Chủ đề","Người gởi", "Ngày"};
            Object DuLieu[][] = new Object[DanhSachMail.size()][cacCotDuLieu.length];
            for(int i = 0; i < DanhSachMail.size();i++){
                int index = 0;
                DuLieu[i][index++] = false;
               //for(int j = 1; i < model.) dulieu[index] = new Object();
                if (i % 2 == 0){

                    DuLieu[i][index++] = icon;
                }
                 if (i % 2 == 0){
                    //Icon icon = new ImageIcon("C:\\1.png");
                    DuLieu[i][index++] = icon;
                }
                DuLieu[i][index++] = DanhSachMail.get(i).getM_NguoiGoi();

                //dulieu[index++] = DanhSachMail.get(i).getM_NguoiGoi();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_ChuDe();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_Ngay().toString();
                
            }
            final Object bangDuLieu[][] = DuLieu;
            DefaultTableModel modelBangHienThi = new DefaultTableModel(bangDuLieu,cacCotDuLieu) {

            @Override
            public Class getColumnClass(int columnIndex) {
                return (columnIndex == 0) ? Boolean.class : String.class;
            }


                @Override
                public int getColumnCount() {
                    return cacCotDuLieu.length;
                }

                @Override
                public String getColumnName(int column) {
                    return cacCotDuLieu[column];
                }

                @Override
                public int getRowCount() {
                    return bangDuLieu.length;
                }

                @Override
                public Object getValueAt(int row, int column) {
                    return bangDuLieu[row][column];
                }

                @Override
                public boolean  isCellEditable(int row, int column) {
                    return column == 0;
                }
            };
            //JTable table = new JTable(modelBangHienThi);
            
            //modelBangHienThi.isCellEditable(1, 1);
            jTable_danhsachmail.setModel(modelBangHienThi);
            
            TableColumn tc = jTable_danhsachmail.getColumnModel().getColumn(0);
            tc.setCellEditor(jTable_danhsachmail.getDefaultEditor(Boolean.class));
            tc.setCellRenderer(jTable_danhsachmail.getDefaultRenderer(Boolean.class));
            tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));

            /*jTable_danhsachmail.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
                    // the method gives the component  like whome the cell must be rendered
                public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean isFocused, int row, int col) {
                    boolean marked = (Boolean) value;
                    JCheckBox rendererComponent = new JCheckBox();
                    if (marked) {
                        rendererComponent.setSelected(true);
                        //JOptionPane.showMessageDialog(null, "F");
                    }
                    return rendererComponent;
                }
            });
            */JTableHeader header = jTable_danhsachmail.getTableHeader();
            header.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                JTableHeader tableHeader = (JTableHeader) e.getSource();
                //tableHeader.isFocusOwner();
                //JOptionPane.showMessageDialog(null, tableHeader.getColumnModel().getColumnIndexAtX(e.getX()));
                if(tableHeader.getColumnModel().getColumnIndexAtX(e.getX())==0)
                    //jTable_danhsachmail.getColumnModel().getColumn(1).s
                    tatCaDuocChon = !tatCaDuocChon;
                    DoiTrangThaiCotCheckBox(tatCaDuocChon);
            }



            public void mousePressed(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        /*
        DefaultTableModel model = (DefaultTableModel)jTable_danhsachmail.getModel();
       // int countcolumn =tableModel.getColumnCount();
      // int index =1;
        model.setRowCount(WIDTH);
        for(int i = 0; i < DanhSachMail.size();i++){
            int index = 1;
            Object dulieu[] = new Object[model.getColumnCount()];
           //for(int j = 1; i < model.) dulieu[index] = new Object();
            dulieu[index++] = DanhSachMail.get(i).getM_NguoiGoi();
            //dulieu[index++] = DanhSachMail.get(i).getM_NguoiGoi();
            dulieu[index++] = DanhSachMail.get(i).getM_ChuDe();
            dulieu[index] = DanhSachMail.get(i).getM_Ngay().toString();
            model.addRow(dulieu);
        }*/
    }

    private void DoiTrangThaiCotCheckBox (boolean isDuocChon){
        TableModel tableModel = jTable_danhsachmail.getModel();
        for (int i = 0; i < tableModel.getRowCount(); i++){
            tableModel.setValueAt(isDuocChon, i, 0);
        }
    }
    class MyItemListener implements ItemListener{
     public void itemStateChanged(ItemEvent e) {
       Object source = e.getSource();
       if (source instanceof AbstractButton == false) return;
       boolean checked = e.getStateChange() == ItemEvent.SELECTED;
       for(int x = 0, y = jTable_danhsachmail.getRowCount(); x < y; x++)
       {
         jTable_danhsachmail.setValueAt(new Boolean(checked),x,0);
       }
     }
   }
}
class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {
    protected CheckBoxHeader rendererComponent;
    protected int column;
    protected boolean mousePressed = false;
    public CheckBoxHeader(ItemListener itemListener) {
     rendererComponent = this;
     rendererComponent.addItemListener(itemListener);
    }
    public Component getTableCellRendererComponent(
        JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
        if (table != null) {
            JTableHeader header = table.getTableHeader();
            if (header != null) {
            rendererComponent.setForeground(header.getForeground());
            rendererComponent.setBackground(header.getBackground());
            rendererComponent.setFont(header.getFont());
            header.addMouseListener(rendererComponent);
            }
        }
         setColumn(column);
         rendererComponent.setText("");
         setBorder(UIManager.getBorder("TableHeader.cellBorder"));
         return rendererComponent;
    }
    protected void setColumn(int column) {
     this.column = column;
    }
    public int getColumn() {
     return column;
    }
    protected void handleClickEvent(MouseEvent e) {
        if (mousePressed) {
           mousePressed=false;
           JTableHeader header = (JTableHeader)(e.getSource());
           JTable tableView = header.getTable();
           TableColumnModel columnModel = tableView.getColumnModel();
           int viewColumn = columnModel.getColumnIndexAtX(e.getX());
           int column = tableView.convertColumnIndexToModel(viewColumn);

           if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
             doClick();
           }
        }
    }
    public void mouseClicked(MouseEvent e) {
     handleClickEvent(e);
     ((JTableHeader)e.getSource()).repaint();
    }
    public void mousePressed(MouseEvent e) {
     mousePressed = true;
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
}

